package com.mszlu.blog.controller;

import com.mszlu.blog.common.aop.LogAnnotation;
import com.mszlu.blog.common.cache.Cache;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.ArticleParam;
import com.mszlu.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Jiang Yizhi
 * @Date: 2022/3/21 1:17 PM
 */
@RestController
@RequestMapping("articles")
public class ArticleController {


    @Autowired
    private ArticleService articleService;

    @PostMapping
    @LogAnnotation(module="文章", operator="获取文章列表")
    @Cache(expire = 5 * 60 * 1000, name = "list_article")
    public Result listArticle(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页：最热文章
     * @return
     */
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000, name = "hot_article")
    public Result hotArticles() {
        int limit = 5;
        return articleService.hotArticles(limit); /** >>> ArticleService hotArticles() */
    }

    /**
     * 首页：最新文章
     * @return
     */
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000, name = "new_article")
    public Result newArticles() {
        int limit = 5;
        return articleService.newArticles(limit); /** >>> ArticleService hotArticles(limit) */
    }

    /**
     * 首页：最新文章
     * @return
     */
    @PostMapping("listArchives")
    public Result listArchives() {
        return articleService.listArchives(); /** >>> ArticleService listArchives() */
    }

    @PostMapping("view/{id}")
    public Result getArticleById(@PathVariable("id") Long articleId) {
        return articleService.getArticleById(articleId);
    }

    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        return articleService.publish(articleParam);
    }
}
